package com.comba.javafxdemo.controller;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

import org.controlsfx.dialog.Dialogs;

import com.comba.javafxdemo.MainApp;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.FileChooser;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class RootLayoutController implements Initializable {
	private MainApp mainApp;

	public void setMainApp(MainApp mainApp) {
		this.mainApp = mainApp;
	}

	@FXML// 私有方法才需要加@FXML注解
	private void handleNew() {
		this.mainApp.getPersonData().clear();
		this.mainApp.setPersonFilePath(null);
	}

	public void handleOpen() {
		FileChooser fileChooser = new FileChooser();
		FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter(
                "XML files (*.xml)", "*.xml");
		fileChooser.getExtensionFilters().add(extFilter);
		File file = fileChooser.showOpenDialog(this.mainApp.getPrimaryStage());
		if(file != null) {
			this.mainApp.loadPersonDataFromFile(file);
		}
	}

	public void handleSave() {
		File personFile = this.mainApp.getPersonFilePath();
		if(personFile != null) {
			this.mainApp.savePersonDataToFile(personFile);
		} else {
			this.handleSaveAs();
		}
	}

	public void handleSaveAs() {
		FileChooser fileChooser = new FileChooser();
		FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter(
                "XML files (*.xml)", "*.xml");
		fileChooser.getExtensionFilters().add(extFilter);
		File file = fileChooser.showSaveDialog(this.mainApp.getPrimaryStage());
		if(file != null) {
			if(!file.getPath().endsWith(".xml")) {
				file = new File(file.getPath() + ".xml");
			}
			this.mainApp.savePersonDataToFile(file);
		}
	}

	public void handleAbout() {
		Dialogs.create()
        .title("AddressApp")
        .masthead("About")
        .message("Author: Marco Jakob\nWebsite: http://code.makery.ch")
        .showInformation();
	}

	public void handleExit() {
		System.exit(0);
	}

	public void showBirthdayStatistics() {
	    try {
	        // Load the fxml file and create a new stage for the popup.
	        FXMLLoader loader = new FXMLLoader();
	        loader.setLocation(MainApp.class.getResource("/com/comba/javafxdemo/view/BirthdayStatistics.fxml"));
	        AnchorPane page = (AnchorPane) loader.load();
	        Stage dialogStage = new Stage();
	        dialogStage.setTitle("Birthday Statistics");
	        dialogStage.initModality(Modality.WINDOW_MODAL);
	        dialogStage.initOwner(this.mainApp.getPrimaryStage());
	        Scene scene = new Scene(page);
	        dialogStage.setScene(scene);

	        // Set the persons into the controller.
	        BirthdayStatisticsController controller = loader.getController();
	        controller.setPersonData(this.mainApp.getPersonData());

	        dialogStage.show();

	    } catch (IOException e) {
	        e.printStackTrace();
	    }
	}

	@Override
	public void initialize(URL location, ResourceBundle resources) {


	}

}
